package juc.thread;

import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch 计数器
 * 两个方法 countDown() await()
 * 当一个或者多个线程调用await时，线程会被阻塞，
 * 直到其他线程调用countDown()方法将计数器减一，直到减至0，这时其他线程接触阻塞状态
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"被灭");
                // 操作完成后调用countDownLatch的countDown()方法，将计数器减一
                countDownLatch.countDown();
            },CountryEnum.forEach_CountryEnum(i).countryName).start();
        }
        // 使当前线程阻塞，等待countDownLatch计数器的数字变成0
        countDownLatch.await();
        System.out.println("秦国一统天下");
    }
}
